var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2016-09-01',
    time: '12:01',
    openid: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this;
    var tt_id = options.tt_id;
    var ty_id = options.ty_id;
    var openid = wx.getStorageSync('openid');
    this.setData({
      openid: openid,
    })
    //console.log(openid);
    this.getTijian_yuyue(tt_id, ty_id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { //下拉刷新
    wx.showNavigationBarLoading();
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
    var that = this;
    var tt_id = data.tt_id;
    var ty_id = data.ty_id;
    this.getTijian_yuyue(tt_id, ty_id);
  },
  getTijian_yuyue: function (tt_id, ty_id) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/Tijian_yuyue',
      'data': {
        tt_id: tt_id,
        ty_id: ty_id,
      },
      success: function (res) {
        console.log(res)
        that.setData({
          tijian_yuyue: res.data.data,
          tt_money: res.data.data.tt_money
        })

      },
      fail: function (err) {
        console.log(err)
      },

    })
  },

  //表单提交 
  formSubmit: function (e) {
    var that = this
    var tijian_yuyue = that.data.tijian_yuyue.ty_name//医院
    var tt_money = that.data.tt_money//价格
    console.log(tt_money)
    var openid = wx.getStorageSync('openid')
    console.log(tijian_yuyue)
    if (e.detail.value.tjyy_name.length == 0 || e.detail.value.tjyy_shenfenzheng.length == 0 || e.detail.value.tjyy_telphone.length == 0) {
      wx.showModal({
        
        content: '姓名、手机号、身份证号不能为空',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: ' 确认提交么？ ',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定');
            var that = this
            var values = e.detail.value
            app.util.request({
              'url': 'entry/wxapp/Pay',
              method: "GET",
              data: {
                openid: openid, z_tw_money: tt_money
              },
              success: function (res) {
                wx.requestPayment({
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr,   //字符串随机数
                  'package': res.data.package,
                  'signType': res.data.signType,
                  'paySign': res.data.paySign,
                  success: function (res) {
                    console.log(res)
                    app.util.request({
                      url: 'entry/wxapp/Tijian_yuyues',
                      data: values,
                      header: { 'Content-Type': 'application/json' },
                      success: function (res) {
                        console.log(res.data)
                        if (res.data.code == 1) {
                          wx.showToast({
                            title: '提交失败',
                          })
                        } else {
                          wx.showModal({
                            title: '提交成功',
                            content: '',
                            success: function (res) {
                              if (res.confirm) {
                                console.log('用户点击确定')
                                wx.navigateTo({
                                  url: '/hyb_yl/tabBar/index/index',
                                })

                              } else if (res.cancel) {
                                console.log('用户点击取消')
                              }
                            }
                          });

                        }
                        
                      },
                      fail: function (err) {
                        console.log(err);
                      }
                    })
                  },
                  fail: function (error) {
                    console.log(error);
                  },
                })
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });

    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
})